<?php


namespace App\Http\Controllers\Business;


use App\Http\Controllers\Controller;
use App\Models\Business\Business;
use App\Models\Business\Food;
use App\Models\Business\FoodClassificationFirst;
use App\Models\Business\FoodClassificationSecond;
use App\Models\Business\FoodClassificationThird;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Validator;
/**
 * @group 商品分类
 *
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */
class FoodClassificationController extends Controller
{
    /**
     * 商品分类查看
     *
     * @queryParam  name 分类名字
     *
     * @bodyParam pid int分类id
     * @bodyParam  level int required 分类等级 1 1级分类  2 2级分类 3 3级分类
     *
     */
    public function index(Request $request){

        if ($request->get('level') == 1){
            $query = FoodClassificationFirst::where(function($q) use($request){
                $request->has('pid') && $q->where('pid',$request->get('pid'));
            })->orderBy('sort','desc');
        }else if ($request->get('level') == 2){
            $query = FoodClassificationSecond::with('parent')->where(function($q) use($request){
                $request->has('pid') && $q->where('pid',$request->get('pid'));
            })->orderBy('sort','desc');
        }else if ($request->get('level') == 3){
            $query = FoodClassificationThird::where(function($q) use($request){
                $request->has('pid') && $q->where('pid',$request->get('pid'));
                $request->has('name') && $q->where('name','like',"%{$request->get('name')}%");
            })->with('parent')->orderBy('sort','desc');
        }else{
            $query = FoodClassificationFirst::with('second')->where(function($q) use($request){
                $request->has('pid') && $q->where('pid',$request->get('pid'));
            });
        }
        return response()->json(pageQuery($query,$request),200);
    }
    /**
     * 主页搜索
     *
     * @bodyParam name 名称
     */
    public function search(Request $request){

        $query=Business::with('food')->where(function ($query) use($request){
                $query->where('status',1);
        })->whereHas('food',function ($q) use($request){
                        $request->has('name') && $q->where('name','like',"%{$request->get('name')}%");
                    })->get()->toArray();
        if ($request->has('name')){
            foreach ($query as $key=>$item){
                unset($query[$key]['password']);
                foreach ($item['food'] as $k=>$food){
                    if(!strstr($food['name'],$request->get('name')))
                        unset($query[$key]['food'][$k]);
                }
            }
            foreach ($query as $k1=>$v){
                $vfood=[];
                foreach ($v['food'] as $k=>$food){
                    $vfood=array_merge($vfood,array($food));
                }
                $query[$k1]['food']=$vfood;
            }
        }
        return response()->json($query,200);
    }
    /**
     * 商品分类信息查看单条
     *
     * @queryParam  name 分类名字
     * @bodyParam  level int required 分类等级 1 1级分类  2 2级分类 3 3级分类
     *
     * 503  没有传参数
     */
    public function show(Request $request,$pk){

        if ($request->get('level') == 1){
            $query = FoodClassificationFirst::find($pk);
        }else if ($request->get('level') == 2){
            $query = FoodClassificationSecond::find($pk);
        }else if ($request->get('level') == 3){
            $query = FoodClassificationThird::find($pk);
        }else{
            return response()->json(["message"=>"没有传参数"],503);
        }
        if ($query==[]){
            return response()->json(["message"=>"没有该信息"],404);
        }
            return response()->json($query,200);
    }
    /**
     * 商品分类添加
     *
     * @bodyParam  name string required 分类名字
     * @bodyParam  pid int required 分类上级
     * @bodyParam  sort int required 排序
     * @bodyParam  level int required 分类等级 1 1级分类  2 2级分类 3 3级分类
     *
     * 503  没有传参数
     * 504  添加失败
     */
    public function store(Request $request)
    {

        $row=$request->all();
        try{
            $e = genValidate();
            $validator = Validator::make($request->all(),$e);
            if($validator->fails()){
                return response()->json($validator->errors()->messages(),400);
            }
            unset($row['level']);
            if ($request->get('level') == 1){
                $query = FoodClassificationFirst::insertGetId($row);
            }else if ($request->get('level') == 2){
                $query = FoodClassificationSecond::insertGetId($row);
            }else if ($request->get('level') == 3){
                $query = FoodClassificationThird::insertGetId($row);
            }else{
                return response()->json(["message"=>"没有传参数"],503);
            }
            if (!$query){
                return response()->json(["message"=>"没有该信息"],404);
            }
            return response()->json(["message"=>"添加成功"],201);
        }catch (QueryException $e){
            return response()->json(["message"=>"添加失败"],504);
        }
    }
    /**
     * 商品分类修改
     *
     * @bodyParam  name string 分类名字
     * @bodyParam  pid int 分类上级
     * @bodyParam  sort int required 排序
     * @bodyParam  level int required 分类等级 1 1级分类  2 2级分类 3 3级分类
     *
     * 503  没有传参数
     * 504 修改失败
     */
    public function update(Request $request, $pk){

        $row=$request->all();
        $e = genValidate();
        $validator = Validator::make($request->all(),$e);
        if($validator->fails()){
            return response()->json($validator->errors()->messages(),400);
        }
        unset($row['level']);
        if ($request->get('level') == 1){
            $query = FoodClassificationFirst::find($pk);
        }else if ($request->get('level') == 2){
            $query = FoodClassificationSecond::find($pk);
        }else if ($request->get('level') == 3){
            $query = FoodClassificationThird::find($pk);
        }else{
            return response()->json(["message"=>"没有传参数"],503);
        }
        if (!$query){
            return response()->json(["message"=>"没有该信息"],404);
        }
        try {
            $query->update($row);
            return response()->json(["message"=>"修改成功"],200);
        }catch (QueryException $e){
            return response()->json(["message"=>"修改失败"],504);
        }
    }
    /**
     * 商品分类删除
     * @bodyParam  level int required 分类等级 1 1级分类  2 2级分类 3 3级分类
     *
     * 503  没有传对应等级分类
     * 504  分类下存在子分类
     */
    public function destroy(Request $request,$pk){

        if ($request->get('level') == 1){
            $data = FoodClassificationFirst::find($pk);
        }
        else if ($request->get('level') == 2){
            $data = FoodClassificationSecond::find($pk);
        }else if ($request->get('level') == 3){
            $data = FoodClassificationThird::find($pk);
        }else{
            return response()->json(["message"=>"没有传对应等级分类"],503);
        }
        if($data == []){
            return response()->json(["message"=>"没有该信息"],404);
        }
        try{
            $data->delete();
            return response()->json(["message" => "删除成功"], 200);
        } catch (\Exception $e) {

            return response()->json(["message" => "分类下存在子分类"], 500);
        }
    }
}
